package com.liu.service.Impl;

import com.liu.mapper.ManageMapper;
import com.liu.pojo.Manager;
import com.liu.pojo.PageBean;
import com.liu.pojo.Video;
import com.liu.pojo.VideoUrl;
import com.liu.service.ManageService;
import com.liu.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Service
public class ManageServiceImpl implements ManageService {
    @Autowired
    private ManageMapper manageMapper;
    @Override
    public Boolean login(Manager manager) {
        if(manageMapper.login(manager) == 1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Boolean upload(Video videoObj) {
        AliOSSUtils aliOSSUtils = new AliOSSUtils();
        String img = null;
        String video = null;
        try {
            img = aliOSSUtils.upload(videoObj.getImg(),0);
            video = aliOSSUtils.upload(videoObj.getVideo(),1);
        } catch (IOException e) {
            log.info("上传失败");
            //上传失败，记录失败日志，并发送失败信息
            return false;
        }
        VideoUrl videoUrl = new VideoUrl(img,video,videoObj.getFileName(), videoObj.getDirector(), videoObj.getLeadingActors(), videoObj.getDescription(),videoObj.getReleaseTime(),videoObj.getLabels(),null);
        manageMapper.upload(videoUrl);
        return true;
    }

    @Override
    public List<VideoUrl> getVideo(PageBean pageBean) {
        pageBean.setStart(LocalDate.of(Integer.parseInt(pageBean.getDate()),1,1));
        pageBean.setEnd(LocalDate.of(Integer.parseInt(pageBean.getDate()),12,30));
        String str = pageBean.getLabels().replace('，','%');
        str = "%" + str + "%";
        pageBean.setLabels(str);
        return manageMapper.getVideo(pageBean);
    }

    @Override
    public Boolean delete(Integer id) {
        VideoUrl videoUrl = manageMapper.getVideoById(id);
        AliOSSUtils aliOSSUtils = new AliOSSUtils();
        videoUrl.setImg(videoUrl.getImg().split(".com/")[1]);
        videoUrl.setVideo(videoUrl.getVideo().split(".com/")[1]);
        try {
            aliOSSUtils.delete(videoUrl.getImg(), videoUrl.getVideo());
        } catch (Exception e) {
           log.info("删除失败，不允许操纵数据库");
           return false;
        }
        manageMapper.delete(id);
        return true;
    }

    @Override
    public VideoUrl getVideoById(Integer id) {
        return manageMapper.getVideoById(id);
    }

    @Override
    public void updateVideo(Video videoObj) {
        VideoUrl old = getVideoById(videoObj.getId());
        AliOSSUtils aliOSSUtils = new AliOSSUtils();
        String img = null;
        String video = null;
        try {
            if(old != null){
                aliOSSUtils.delete(old.getImg().split(".com/")[1], old.getVideo().split(".com/")[1]);
                img = aliOSSUtils.upload(videoObj.getImg(),0);
                video = aliOSSUtils.upload(videoObj.getVideo(), 1);
            }
        } catch (Exception e) {
            //异常处理
            System.out.println("===========================");
        }
        VideoUrl videoUrl = new VideoUrl(img,video,videoObj.getFileName(),videoObj.getDirector(),
                videoObj.getLeadingActors(),videoObj.getDescription(),videoObj.getReleaseTime(), videoObj.getLabels(),videoObj.getId());
        manageMapper.update(videoUrl);

    }


}
